package com;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import connection.DBConnection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class LedgerReport
 */
public class LedgerReport extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LedgerReport() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String value1=request.getParameter("comboboxfieldname");
		System.out.println("Ledger name-"+value1);
		
		String value2=request.getParameter("code");
		System.out.println("Ledger code-"+value2);
		
	//	DBManipulation dbc=new DBManipulation();
	//	dbc.checkLedger(value1);
		
		RequestDispatcher requestDispatcher;
		
		if(value1.equals("Sales Net Amount"))
		{
			requestDispatcher = request.getRequestDispatcher("SalesNetAmount.jsp" );
	        requestDispatcher.forward(request,response);
		}
		else if(value1.equals("Sales Tax Amount"))
		{
			requestDispatcher = request.getRequestDispatcher("SalesTaxAmount.jsp" ) ;
	        requestDispatcher.forward(request,response);
		}
		else if(value1.equals("Sales Transport Amount"))
		{
			requestDispatcher = request.getRequestDispatcher("SalesTransportAmount.jsp" ) ;
	        requestDispatcher.forward(request,response);
		}
		else if(value1.equals("Purchase Net Amount"))
		{
			requestDispatcher = request.getRequestDispatcher("PurchaseNetAmount.jsp" ) ;
	        requestDispatcher.forward(request,response);
		}
		else if(value1.equals("Purchase Tax Amount"))
		{
			requestDispatcher = request.getRequestDispatcher("PurchaseTaxAmount.jsp" ) ;
	        requestDispatcher.forward(request,response);
		}
		else if(value1.equals("Purchase Transport Amount"))
		{
			requestDispatcher = request.getRequestDispatcher("PurchaseTransportAmount.jsp" ) ;
	        requestDispatcher.forward(request,response);
		}
		else if(value1.equals("Purchase Other Costs"))
		{
			requestDispatcher = request.getRequestDispatcher("PurchaseOtherCosts.jsp" ) ;
	        requestDispatcher.forward(request,response);
		}
		else
		{
		
		DBConnection dbc=new DBConnection();
		Connection con=dbc.getNewConnection();
		
		Statement st=null;
		ResultSet rs=null;
		
		String group="";
		
		try
		{
			
			String sql= "select cname from customermaster where cname='"+value1+"' ";
			st = con.createStatement();
			rs=st.executeQuery(sql);
				
				if(rs.next())
				{
						group="Sundry Debtors";
						
						request.setAttribute("accountname",value1);
						request.setAttribute("accountcode",value2);
						request.setAttribute("accountgroup",group);
				         
				        requestDispatcher = request.getRequestDispatcher("CustomerLedgerReport.jsp" ) ;
				        requestDispatcher.forward(request,response);		       
				}
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}	
		
		try
		{
			
			String sql= " select sname from suppliermaster where sname='"+value1+"' ";
			st = con.createStatement();
			rs=st.executeQuery(sql);
				
				if(rs.next())
				{
						group="Sundry Creditors";
						
						request.setAttribute("accountname",value1);
						request.setAttribute("accountcode",value2);
						request.setAttribute("accountgroup",group);
				         
				        requestDispatcher = request.getRequestDispatcher("SupplierLedgerReport.jsp" ) ;
				        requestDispatcher.forward(request,response);
				}
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}		
		
		try
		{
			
			String sql= " select gname from GeneralLedger2 where gname='"+value1+"' ";
			st = con.createStatement();
			rs=st.executeQuery(sql);
				
				if(rs.next())
				{
						//group="Sundry Creditors";
						
						request.setAttribute("accountname",value1);
						request.setAttribute("accountcode",value2);
						request.setAttribute("accountgroup",group);
				         
				        requestDispatcher = request.getRequestDispatcher("GeneralLedgerReport.jsp" ) ;
				        requestDispatcher.forward(request,response);
				}
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}

		try
		{
			String sql= " select bname from BankMaster where bname='"+value1+"' ";
			st = con.createStatement();
			rs=st.executeQuery(sql);
				
				if(rs.next())
				{
						//group="Sundry Creditors";
						
						request.setAttribute("bankname",value1);
						//request.setAttribute("accountcode",value2);
						//request.setAttribute("accountgroup",group);
				         
				        requestDispatcher = request.getRequestDispatcher("BankLedgerReport.jsp" ) ;
				        requestDispatcher.forward(request,response);
				}
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}

		} // end of else
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
